package com.rgs.base.database.config;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Lists;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author Ren Gaoshuai
 * @ClassName DruidDataAutoConfig
 * @Description 数据库连接配置类
 * @date 2021-12-24 9:49
 **/
@Configuration
@ConditionalOnProperty(prefix = "spring.druid",
        name = "enable", havingValue = "true")
@Import(DruidImportSelector.class)
public class DruidDataAutoConfig {

    public DruidDataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        //设置慢查询
        dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
        return dataSource;
    }

    public Filter statFilter() {
        StatFilter filter = new StatFilter();
        // 设置慢sql时间
        filter.setSlowSqlMillis(3000);
        filter.setLogSlowSql(true);
        filter.setMergeSql(true);
        return filter;
    }
}
